<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Click Link Enter Text Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<style>
  input { width: 100px; }
  .alink { text-decoration: underline; color: blue; cursor: pointer; }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // 2000 seconds

// click-link
// since we randomly generate links, run this in a while loop
// until we produce text that definitely contains a link,
// otherwise the script will error out and fail.
var createLinks = function(div){
  var linkCreated = false;
  while(!linkCreated){
    var txt =  ui_utils.generateWords(20).split(/\s/g);
    for(var j=0;j<txt.length; j++){
      if(Math.random() < 0.2){
        txt[j] = '<span class="alink">' + txt[j] + '</span>';
        linkCreated = true;
      }
    }
  }

  div.html(txt.join(' '));
  return txt;
}

// enter-text
var randomizeInputs = function(){
  // move the text field around
  var s = '';
  s += 'margin-left:'+core.randi(0,20)+'px;';
  s += 'margin-top:'+core.randi(0,20)+'px;';
  d3.select('#form').attr('style', s);

  // and submit button a bit too
  d3.select('#subbtn').attr('style', 'margin-top:'+core.randi(0,20)+'px;');
}

var genProblem = function() {
  // generate the task
  var div = d3.select('#area');
  div.html(''); // clear previous problem, if any
  var correctText = createLinks(div);

  var any = core.getOpt(core.QueryString, 'any', false); // click Any link?

  var link_state = -1.0
  var elements = document.getElementsByClassName('alink');
  var correctIndex = core.randi(0, elements.length);
  var correctText = elements[correctIndex].innerHTML;
  for(var i = 0, len = elements.length; i < len; i++) {
    var e = elements[i];
    if(e.innerHTML === correctText) {
      d3.select(e).on('click', function(){ link_state = 1.0; })
    } else {
      d3.select(e).on('click', function(){ link_state = -1.0; })
    }
  }

  // enter-text
  div.append('br')
  div.append('div').attr('id', 'form')
  var div_form = d3.select('#form');
  div_form.html('');
  div_form.append('input').attr('type', 'text').attr('id', 'tt')
  div_form.append('button').html('Submit').attr('id', 'subbtn').attr('class', 'secondary-action');
  d3.select('#tt')[0][0].value ='';
  randomizeInputs();
  var q = core.sample(ui_utils.FIFTY_NAMES);

  // generate query text in the UI
  d3.select('#query').html('Click on the link "' + correctText + '", and then enter "<span class="bold">' + q + '</span>" into the text field and press Submit.');
  // d3.select('#query').html('Enter "<span class="bold">' + q + '</span>" into the text field and press Submit, after clicking on the link "' + correctText + '".');

  d3.select('#subbtn').on('click', function(){
    var t = d3.select('#tt')[0][0].value;
    var r = t === q ? 1.0 : -1.0;
    if (link_state == 1.0) {
        core.endEpisode(r, r > 0);
    } else {
        core.endEpisode(-1.0);
    }
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area"></div>
</div>
</body>
</html>
